package com.qubuyer.core.app

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.lifecycle.ViewModelProvider
import com.qubuyer.core.framework.BaseViewModel

/**
 * Author: linwg
 * Time: 2020/10/12 0012
 * Description:
 */
abstract class VMFragment<VM : BaseViewModel> : BaseFragment() {
    protected abstract val viewModel: VM

    inline fun <reified VM : BaseViewModel> injectVMByFragment(
            clazz: Class<VM> = VM::class.java
    ): Lazy<VM> {
        return lazy {
            val vm = ViewModelProvider(this).get(clazz)
            vm.lifecycleOwner = this
            vm
        }
    }

    inline fun <reified VM : BaseViewModel> injectVMByActivity(
            clazz: Class<VM> = VM::class.java
    ): Lazy<VM> {
        return lazy {
            val vm = ViewModelProvider(mContext).get(clazz)
            vm.lifecycleOwner = this
            vm
        }
    }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        mRoot = inflater.inflate(layoutId, container, false)
        return mRoot
    }
}